package com.dream.system.action;

import com.dream.common.Msg;
import com.dream.common.SuperController;
import com.dream.model.SysUser;
import com.dream.system.service.ISysUser;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping({ "/system/loginAction.do" })
public class LoginAction extends SuperController {

	@Autowired
	private ISysUser sysUserImpl;

	@RequestMapping(params = { "method=login" })
	public void login(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		Object bl = isAdmin(model.getTell(), model.getUserPwd());
		
		response.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		if ((model.getTell() == null) || ("".equals(model.getTell()))) {
			Msg msg = getMsg("0", "用户不能为空!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		if ((model.getUserPwd() == null) || ("".equals(model.getUserPwd()))) {
			Msg msg = getMsg("0", "密码不能为空!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		if ((model.getUserType() == null) || ("".equals(model.getUserType()))) {
			Msg msg = getMsg("0", "用户类型不能为空!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}

		if (bl != null) {
			SysUser su = (SysUser) bl;
			su.setUserType("6");
			request.getSession().setAttribute("sysUser", su);
			Msg msg = getMsg("1", "用户名或密码不正确!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}

		int temp = this.sysUserImpl.getUserlogin(model, request);
		if (temp == 1) {
			Msg msg = getMsg("1", "登陆成功!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		} else {
			Msg msg = getMsg("0", "操作失败!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		}
	}

	@RequestMapping(params = { "method=loginUrl" })
	public String loginUrl(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		SysUser su = (SysUser) request.getSession().getAttribute("sysUser");
		if (null == su) {
			return "/login";
		}
		if ("2".equals(su.getUserType())) {
			return "/teacherIndex";
		}
		if("5".equals(su.getUserType())){
			return "/zhuanjia";
		}
		if ("1".equals(su.getUserType())) {
			return "/studentIndex";
		}
		return "/index";
	}

}